package com.yxinsur.imc.web.controller.business;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yxinsur.imc.business.domain.ImcBrokerPersistencyRate;
import com.yxinsur.imc.business.form.BrokerPersistencyRateCalculateForm;
import com.yxinsur.imc.business.form.BrokerPersistencyRateForm;
import com.yxinsur.imc.business.service.IImcBrokerPersistencyRateService;
import com.yxinsur.imc.business.vo.BrokerPersistencyRateDetailVo;
import com.yxinsur.imc.business.vo.BrokerPersistencyRateVo;
import com.yxinsur.imc.common.core.controller.BaseController;
import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.core.page.TableDataInfo;
import com.yxinsur.imc.common.utils.StringUtils;
import com.yxinsur.imc.common.utils.poi.ExcelUtil;
import com.yxinsur.imc.common.validator.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 代理人继续率Controller
 * 
 * @author imc
 * @date 2021-05-20
 */
@Api(value = "代理人继续率管理",tags = "7.代理人继续率管理")
@RestController
@RequestMapping("/business/persistency/rate")
public class ImcBrokerPersistencyRateController extends BaseController
{
    @Autowired
    private IImcBrokerPersistencyRateService imcBrokerPersistencyRateService;

    /**
     * 查询代理人继续率列表
     */
    @ApiOperation("代理人继续率列表分页查询")
    @GetMapping("/list")
    public TableDataInfo list(BrokerPersistencyRateForm brokerPersistencyRateForm)
    {
        startPage();
        List<BrokerPersistencyRateVo> list = imcBrokerPersistencyRateService.selectImcBrokerPersistencyRateList(brokerPersistencyRateForm);
        return getDataTable(list);
    }
    /**
     * 继续率详情列表分页查询
     */
    @ApiOperation("继续率详情列表分页查询")
    @GetMapping("/{id}/detaillist")
    public TableDataInfo detaillist(@PathVariable Long id)
    {
        ImcBrokerPersistencyRate rate = this.imcBrokerPersistencyRateService.getById(id);
        Assert.isNull(rate,"非法的继续率ID");
        List<BrokerPersistencyRateDetailVo> vos  = new ArrayList<>();
        if(StringUtils.isNotBlank(rate.getPayableOrderRiskJson())){
            vos = JSONArray.parseArray(rate.getPayableOrderRiskJson(), BrokerPersistencyRateDetailVo.class);
        }
        return getDataTable(vos);
    }

    /**
     * 导出代理人继续率列表
     */
    @ApiOperation("代理人继续率列表数据导出查询")
    @GetMapping("/export")
    public AjaxResult export(BrokerPersistencyRateForm brokerPersistencyRateForm)
    {
        List<BrokerPersistencyRateVo> list = imcBrokerPersistencyRateService.selectImcBrokerPersistencyRateList(brokerPersistencyRateForm);
        ExcelUtil<BrokerPersistencyRateVo> util = new ExcelUtil<BrokerPersistencyRateVo>(BrokerPersistencyRateVo.class);
        return util.exportExcel(list, "rate");
    }

    /**
     * 继续率计算
     */
    @ApiOperation("继续率计算")
    @PostMapping("/calculate")
    public AjaxResult add(@RequestBody BrokerPersistencyRateCalculateForm brokerPersistencyRateCalculateForm)
    {
        this.imcBrokerPersistencyRateService.calculate(brokerPersistencyRateCalculateForm);
        return AjaxResult.success();
    }


    /**
     * 删除代理人继续率
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(imcBrokerPersistencyRateService.deleteImcBrokerPersistencyRateByIds(ids));
    }
}
